﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows;

using Microsoft.Win32;

namespace Outcoder.Calcium.VSIntegration.Installation
{
	[RunInstaller(true)]
	public partial class VSTemplateInstaller : Installer
	{
		public VSTemplateInstaller()
		{
			InitializeComponent();
		}

		string VSVersion
		{
			get
			{
				const string vsVersionKey = "VSVersion";
				if (Context.Parameters.ContainsKey("vsVersionKey"))
				{
					LogAction("VSVersion parameter not supplied.");
					return null;
				}
				return Context.Parameters[vsVersionKey];
			}
		}

		bool Legacy
		{
			get
			{
				const string legacyKey = "Legacy";
				if (!Context.Parameters.ContainsKey(legacyKey))
				{
					return false;
				}
				string legacyValue = Context.Parameters[legacyKey];
				return legacyValue != null && legacyValue.ToLower() == "true";
			}
		}

		Action<string> LogAction
		{
			get
			{
				return message => Context.LogMessage(message);
			}
		}

		void ShowMessage(string message)
		{
			MessageBox.Show(message);
		}

		IEnumerable<string> TemplateFiles
		{
			get
			{
				string parameter = Context.Parameters["Templates"];
				if (string.IsNullOrWhiteSpace(parameter))
				{
					return new List<string>();
				}
				if (parameter.EndsWith("/"))
				{
					parameter = parameter.Substring(0, parameter.Length - 1);
				}
				string[] result = parameter.Split(new[] {';'});
				return result;
			}
		}

		bool InstallVSTemplates
		{
			get
			{
				string parameter = Context.Parameters["InstallVSTemplates"];
				return parameter != null && parameter.ToLower() == "true";
			}
		}

		public override void Install(IDictionary stateSaver)
		{
			base.Install(stateSaver);
			//MessageBox.Show("Install");
//			foreach (string templateFile in TemplateFiles)
//			{
//				MessageBox.Show(templateFile);
//			}
		}

		public override void Commit(IDictionary savedState)
		{
			base.Commit(savedState);
			//MessageBox.Show("Commit");
			if (Legacy)
			{
				LegacyInstaller.CommitLegacy(
					VSVersion, LogAction);
				return;
			}

			if (InstallVSTemplates)
			{
				Tools.RunInstallVSTemplates(VSVersion, LogAction);
			}
		}

		public override void Rollback(IDictionary savedState)
		{
			//base.Rollback(savedState);
			//MessageBox.Show("Rollback");
		}

		public override void Uninstall(IDictionary savedState)
		{
			base.Uninstall(savedState);
			//MessageBox.Show("Uninstall custom action.");

			if (Legacy)
			{
				LegacyInstaller.UninstallLegacy(
					VSVersion, message => Context.LogMessage(message));
				return;
			}

			//MessageBox.Show("Uninstall");

			foreach (string fileName in TemplateFiles)
			{
				//MessageBox.Show("Deleting file " + fileName);
				bool deleted = Tools.DeleteFileIfExists(fileName);
				//ShowMessage(deleted ? "Deleted." : "File was not deleted");
			}

			if (Environment.MachineName.ToLower() == "energia")
			{
				MessageBoxResult result = MessageBox.Show("Run InstallVSTemplates?", "Calcium Installer", MessageBoxButton.YesNo);
				if (result == MessageBoxResult.Yes || result == MessageBoxResult.OK)
				{
					Tools.RunInstallVSTemplates(VSVersion, LogAction);
				}
			}
			else
			{
				Tools.RunInstallVSTemplates(VSVersion, LogAction);
			}
		}
		
	}
}
